﻿//---------------------------------------------------------------------
//  Copyright (c) 2012 Daun Project.  All rights reserved.
//  http://daun.codeplex.com
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Daun.Cores;
using Daun.Controls.TreeGridView;
using Daun.Forms;
using Daun.Cores.Entities;
namespace Daun.Controls
{
    public partial class DocumentUserControl : UserControl
    {
        private MongoEngine _engine;
        public string CollectionName { set; get; }
        public string DatabaseName { set; get; }
        public string MongoPath { set; get; }
        public MongoEngine DBEngine
        {
            set
            {
                _engine = value;
            }
        }
        public DocumentUserControl()
        {
            InitializeComponent();
        }

        public void Populate()
        {
            Dictionary<int,List<Daun.Cores.Entities.Document>> list = _engine.GetCollectionDataList(DatabaseName, CollectionName);

            foreach (KeyValuePair<int, List<Daun.Cores.Entities.Document>> item in list)
            {
                TreeGridNode node = treeGridViewDoc.Nodes.Add(string.Format("[{0}]<..>",item.Key), "", "");
                node.ImageIndex = 0;
                node.Tag = item.Value;
                foreach (var col in item.Value)
                {
                    TreeGridNode node1 = node.Nodes.Add(col.Key, col.Value, col.DocumentType);
                    node1.ImageIndex = 1;
                    node1.Tag = item.Value;
                }

            }
            
        }

        public void RefreshData()
        {
            treeGridViewDoc.Nodes.Clear();
            Populate();
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // edit item
            if (treeGridViewDoc.SelectedRows.Count > 0)
            {
                TreeGridNode node = treeGridViewDoc.GetNodeForRow(treeGridViewDoc.SelectedRows[0]);                
                DocumentForm frm = new DocumentForm();
                frm.Mode = FormMode.Edit;
                frm.CollectionName = CollectionName;
                frm.DatabaseName = DatabaseName;
                frm.Documents = (List<Daun.Cores.Entities.Document>)node.Tag;                
                frm.Text = String.Format("Edit Document {0}/{1}", DatabaseName,CollectionName);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    DocumentProperty doc = frm.SelectedObject;
                    List<Document> newDoc = doc.GetDocuments();
                    _engine.UpdateDocument(DatabaseName, CollectionName, (List<Daun.Cores.Entities.Document>)node.Tag,newDoc);

                    RefreshData();
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // delete item
            if (treeGridViewDoc.SelectedRows.Count > 0)
            {
                DialogResult ret = MessageBox.Show("Are you sure to delete this selected item?",
                    "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                if (ret == DialogResult.OK)
                {
                    TreeGridNode node = treeGridViewDoc.GetNodeForRow(treeGridViewDoc.SelectedRows[0]);
                    _engine.DeleteDocument(DatabaseName, CollectionName, (List<Daun.Cores.Entities.Document>)node.Tag);
                    RefreshData();
                }
            }
        }
    }
}
